import Promise from 'promise'

ConfigContext.prototype.key = 'test-mng'
ConfigContext.prototype.name = '测试管理页'
// 配置 path, 可以是array或者对象
ConfigContext.prototype.routeConfig = {path: '/test', parentPageKey: null}
function ConfigContext () {
  // 当列表中有选择项时，此数据会自动设置值
  this.selected = null // {}/[]/null
  this.checked = null // []/null

  let $this = this
  let createFormSchema = {
    formClass: 'va-df-form-horizontal-60 va-df-form-grid-4',
    fields: [
      {
        componentType: 'InputText',
        name: 'name',
        label: '名称'
      },
      {
        componentType: 'InputTextarea',
        name: 'description',
        label: '描述',
        textareaRows: 6
      },
      {
        componentType: 'InputCombo',
        name: 'type',
        label: '用户类型',
        dataSource () {
          return [
            {
              id: 1,
              name: '修理厂'
            },
            {
              id: 2,
              name: '汽配商'
            },
            {
              id: 3,
              name: '4S店'
            },
            {
              id: 4,
              name: '保险公司'
            }
          ]
        },
        comboTableFullWidth: true,
        valueProp: 'id',
        displayProp: 'name',
        comboColumns: [{
          field: 'name'
        }],
        validators: {
          required: true
        }
      }
    ]
  }
  let updateFormSchema = {
    formClass: 'va-df-form-horizontal-60 va-df-form-grid-2',
    fields: [
      {
        componentType: 'InputText',
        name: 'name',
        label: '名称',
        validators: {
          required: true,
          maxLength: 20
        }
      },
      {
        componentType: 'InputTextarea',
        name: 'description',
        label: '描述',
        textareaRows: 6
      },
      {
        componentType: 'InputCombo',
        name: 'type',
        label: '用户类型',
        dataSource () {
          return [
            {
              id: 1,
              name: '修理厂'
            },
            {
              id: 2,
              name: '汽配商'
            },
            {
              id: 3,
              name: '4S店'
            },
            {
              id: 4,
              name: '保险公司'
            }
          ]
        },
        comboTableFullWidth: true,
        valueProp: 'id',
        displayProp: 'name',
        comboColumns: [{
          field: 'name'
        }],
        validators: {
          required: true
        }
      }
    ],
    submitHandler (data) {
      return new Promise((resolve) => {
        console.log('updateFormSchema submit', data)
        setTimeout(() => {
          resolve(false)
        }, 20000)
      })
      // console.log('updateFormSchema submit', data)
      // return false
    }
  }

  $this.config = {
    key: 'test-mng',
    onRouteChange () { console.log('onRouteChange', $this.$context.$route) }, // 用于当路由参数变化时，在此函数中处理相关逻辑
    onCreated () { console.log('onCreated') }, // 用于组件创建时调用
    onMounted () { console.log('onMounted', $this.$context.$route) }, // 用于组件加载时调用
    onDestroy () { console.log('onDestroy') }, // 用于组件销毁时调用
    search: {
      cache: true,
      defaultModel: {},
      formClass: 'va-df-form-horizontal-60 va-df-form-inline',
      fields: [
        {
          componentType: 'InputText',
          name: 'keyword',
          label: '名称'
        }
      ]
    },
    globalActions: [
      {
        label: '创建',
        buttonClass: 'btn btn-primary',
        formTitle: '添加信息',
        formSchema: createFormSchema
      }
    ],
    grid: {
      // httpMethod: 'GET',
      // dataSourceUrl: `test/list`,
      // dataSourcePreDeal (data) {
      //   return {
      //     data: data.items,
      //     totalCount: data.pagination.totalCount
      //   }
      // }, // data参数为dataSourceUrl返回的数据
      autoLoad: true,
      sortRemote: false,
      dataSource: [{id: 1, name: 'name01'}, {id: 2, name: 'name02'}],
      headerHeights: [30],
      rowHeight: 30,
      columns: [
        {
          field: 'id',
          title: '编号',
          width: 120,
          sortable: true
        },
        {
          field: 'name',
          title: '名称',
          sortable: true
        }
      ],
      actionColumnWidth: 340,
      actions: [
        {
          label: '修改',
          buttonClass: 'btn btn-xs btn-primary',
          formTitle: '修改信息',
          actionDataPreDeal (data) {
            console.log('actionDataPreDeal', data)
            return data
          },
          formDataPreDeal (formData) {
            console.log('formDataPreDeal', formData)
            return formData
          },
          formSchema: updateFormSchema,
          autoCloseModal: true,
          disableEscClose: true,
          showClose: false
        },
        {
          label: '删除',
          buttonClass: 'btn btn-xs btn-danger',
          preConfirm: {
            title: '删除确定',
            message: '是否删除此商品？'
          },
          actionDataPreDeal (data) {
            console.log('actionDataPreDeal', data)
            return data
          },
          customOperation (data) {
            console.log('customOperation', data)
          },
          modalWidth: '320px',
          staticModal: false
        }
      ],
      pagination: {}
    }
  }
}

export default ConfigContext
